﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FonShare.Classes;
using FonShare.Classes.Data;

namespace FonShare.WebPages
{
    public partial class ManageGroups : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadMyGroups();
                LoadJoinGroup();
            }
        }

        protected void MyGroupButton_Click(object sender, EventArgs e)
        {
            if (MyGroupListBox.SelectedItem != null)
            {
                FS_DataClassesDataContext dc = new FS_DataClassesDataContext();
                FS_Group groupTodelete = dc.FS_Groups.Where(record => record.GroupID == Int32.Parse(MyGroupListBox.SelectedItem.Value)).First();
                dc.FS_Groups.DeleteOnSubmit(groupTodelete);
                dc.SubmitChanges();
            }
        }

        protected void JoinGroupButton_Click(object sender, EventArgs e)
        {
            if (JoinGroupListBox.SelectedItem != null)
            {
                int userId = (int) Session[FS_Constants.Session.USERID];
                FS_DataClassesDataContext dc = new FS_DataClassesDataContext();
                FS_GroupUser groupUserTodelete = dc.FS_GroupUsers.Where(record => record.GroupID == Int32.Parse(MyGroupListBox.SelectedItem.Value) && record.UserID == userId).First();
                dc.FS_GroupUsers.DeleteOnSubmit(groupUserTodelete);
                dc.SubmitChanges();
            }
        }

        private void LoadMyGroups()
        {
            int userId = (int)Session[FS_Constants.Session.USERID];
            FS_DataClassesDataContext dc = new FS_DataClassesDataContext();
            MyGroupListBox.Items.Clear();

            //Create myGroupsList.
            List<FS_Group> myGroupsList = dc.FS_Groups.Where(record => record.OwnerID == userId).ToList();
            foreach (FS_Group currentGroup in myGroupsList)
            {
                MyGroupListBox.Items.Add(new ListItem(currentGroup.GroupName + " (" + currentGroup.FS_GroupUsers.Count + ")", currentGroup.GroupID.ToString()));
            }
        }

        private void LoadJoinGroup()
        {
            int userId = (int)Session[FS_Constants.Session.USERID];
            FS_DataClassesDataContext dc = new FS_DataClassesDataContext();
            JoinGroupListBox.Items.Clear();

            //Create JoinGroupList.
            List<FS_Group> joinGroupList = dc.FS_GroupUsers.Where(record => record.UserID == userId).Select(record => record.FS_Group).ToList();
            foreach (FS_Group currentGroup in joinGroupList)
            {
                JoinGroupListBox.Items.Add(new ListItem(currentGroup.GroupName + " (" + currentGroup.FS_GroupUsers.Count + ")", currentGroup.GroupID.ToString()));
            }
        }
    }
}
